<?php
App::import('Sanitize');
class CommonHelper extends AppHelper {
    var $helpers = array('Html', 'Time', 'Text', 'Javascript');
    
    var $__banglaDay = array('5'=>'শুক্রবার','6'=>'শনিবার','0'=>'রোববার','1'=>'সোমবার','2'=>'মঙ্গলবার','3'=>'বুধবার','4'=>'বৃহস্পতিবার');
    var $__banglaDayShort = array('5'=>'শুক্র','6'=>'শনি','0'=>'রোব','1'=>'সোম','2'=>'মঙ্গল','3'=>'বুধ','4'=>'বৃহ');
    var $__banglaNumber =array('0'=>'০','1'=>'১','2'=>'২','3'=>'৩','4'=>'৪','5'=>'৫','6'=>'৬','7'=>'৭','8'=>'৮','9'=>'৯');
    var $__banglaGerMonth = array('01'=>'জানুয়ারি','02'=>'ফেব্রুয়ারি','03'=>'মার্চ','04'=>'এপ্রিল','05'=>'মে','06'=>'জুন' ,'07'=>'জুলাই','08'=>'আগস্ট','09'=>'সেপ্টেম্বর','10'=>'অক্টোবর','11'=>'নভেম্বর','12'=>'ডিসেম্বর');
    var $__banglaMonth = array('বৈশাখ','জৈষ্ঠ্য','আষাঢ়','শ্রাবণ','ভাদ্র','আশ্বিন','কার্তিক','অগ্রাহায়ন','পৌষ','মাঘ','ফাল্গুন','চৈত্র');
    
    
    function _banglaDate($date){
        return $this->_banglaDay(date('w',strtotime($date))).', '.$this->_banglaNumber(date('j',strtotime($date))).' '.$this->_banglaMonth(date('m',strtotime($date))).' '.$this->_banglaNumber(date('Y',strtotime($date))); 
        
    }
    function _banglaDay($v,$isShort=false){
        
        return $isShort?$this->__banglaDayShort[$v]:$this->__banglaDay[$v];
        }
    function _banglaNumber($v){
        
        for( $i=0; $i<10; $i++ )
            $v = str_replace("$i",$this->__banglaNumber[$i],$v);
        return $v;
        }
    function _banglaMonth($v){
        
        return $this->__banglaGerMonth[$v];
        }


    
    function js($scripts, $extra=''){
        
        $cachepath = JS . $extra . 'main.js';
        if(!is_dir(dirname($cachepath))) :
            mkdir(dirname($cachepath));
        endif;
        
        $buffer = '';
        if(is_array($scripts)) :
            $cacheOk = true;
            foreach($scripts as $script) :
                $filepath = JS.$script.'.js';
                
                if(!file_exists($filepath)) :
                    die($filepath.' - Wrong file name.');
                endif;
                
                if(file_exists($cachepath)) :
                    $fileModified = filemtime($filepath);
                    $cacheModified = filemtime($cachepath);
                    if($fileModified > $cacheModified) :
                        $cacheOk = false;
                    endif;
                else:
                    $cacheOk = false;
                endif;
                $buffer .= file_get_contents($filepath);
            endforeach;
        endif;
        
        if(!$cacheOk) :
            if (!class_exists('File')) :
                uses('file');
            endif;
            
            App::import('Vendor', 'jsmin' . DS . 'jsmin');
            $cacheData = JSMin::minify($buffer);
            
            $cache = new File($cachepath);
            $cache->write($cacheData);        
            $fileModified = time();
        endif;
        return $this->Javascript->link($extra.'main');
    }
    
    function css($scripts, $extra=''){
        
        $cachepath = CSS . $extra . 'main.css';
        if(!is_dir(dirname($cachepath))) :
            mkdir(dirname($cachepath));
        endif;
        
        $buffer = '';
        if(is_array($scripts)) :
            $cacheOk = true;
            foreach($scripts as $script) :
                $filepath = WWW_ROOT.$script.'.css';
                
                if(!file_exists($filepath)) :
                    die($filepath.' - Wrong file name.');
                endif;
                
                if(file_exists($cachepath)) :
                    $fileModified = filemtime($filepath);
                    $cacheModified = filemtime($cachepath);
                    if($fileModified > $cacheModified) :
                        $cacheOk = false;
                    endif;
                else:
                    $cacheOk = false;
                endif;
                $buffer .= file_get_contents($filepath);
            endforeach;
        endif;
        
        if(!$cacheOk) :
            if (!class_exists('File')) :
                uses('file');
            endif;
            
            App::import('Vendor', 'cssmin' . DS . 'cssmin');
            $cacheData = CssMin::minify($buffer);
            
            $cache = new File($cachepath);
            $cache->write($cacheData);        
            $fileModified = time();
        endif;
        return $this->Html->css($extra.'main');
    }
    
    
    
    
    function name($user) {
        if(!empty($user['User']['name'])) return html_entity_decode($user['User']['name']);
        if(!empty($user['User']['username'])) return $user['User']['username'];
        return '';
    }
    
    function toBnNum($num) {
        return str_replace(
            array('0','1','2','3','4','5','6','7','8','9'), 
            array('০','১','২','৩','৪','৫','৬','৭','৮','৯',), 
            $num);
    }
    
    function bnDate($time = '') {
        if($time == '') $time = time();
        $arrBnMon = array(
            'বৈশাখ',
            'জ্যৈষ্ঠ',
            'আষাঢ়',
            'শ্রাবণ',
            'ভাদ্র',
            'আশ্বিন',
            'কার্তিক',
            'অগ্রহায়ণ',
            'পৌষ',
            'মাঘ',
            'ফাল্গুন',
            'চৈত্র',
        );
        
        $day    = date('j', $time);
        $mon    = date('n', $time);
        $year   = date('Y', $time);
        
        $ly = 0;
        if($year%400 ==0 || ($year%100 != 0 && $year%4 == 0)) $ly = 1;
        
        $bnMon = -1;
        $bnDay = -1;
        $yx = 0;
        
        if($day >= 14 && $mon == 4) :
            $bnMon = 0;
            $bnDay = $day - 13;
        elseif($day <= 14 && $mon == 5) :
            $bnMon = 0;
            $bnDay = $day + 17;
        elseif($day > 14 && $mon == 5) :
            $bnMon = 1;
            $bnDay = $day - 14;
        elseif($day <= 14 && $mon == 6) :
            $bnMon = 1;
            $bnDay = $day + 17;
        elseif($day > 14 && $mon == 6) :
            $bnMon = 2;
            $bnDay = $day - 14;
        elseif($day <= 15 && $mon == 7) :
            $bnMon = 2;
            $bnDay = $day + 16;
        elseif($day > 15 && $mon == 7) :
            $bnMon = 3;
            $bnDay = $day - 15;
        elseif($day <= 15 && $mon == 8) :
            $bnMon = 3;
            $bnDay = $day + 16;
        elseif($day > 15 && $mon == 8) :
            $bnMon = 4;
            $bnDay = $day - 15;
        elseif($day <= 15 && $mon == 9) :
            $bnMon = 4;
            $bnDay = $day + 16;
        elseif($day > 15 && $mon == 9) :
            $bnMon = 5;
            $bnDay = $day - 15;
        elseif($day <= 16 && $mon == 10) :
            $bnMon = 5;
            $bnDay = $day + 15;
        elseif($day > 16 && $mon == 10) :
            $bnMon = 6;
            $bnDay = $day - 16;
        elseif($day <= 15 && $mon == 11) :
            $bnMon = 6;
            $bnDay = $day + 15;
        elseif($day > 15 && $mon == 11) :
            $bnMon = 7;
            $bnDay = $day - 15;
        elseif($day <= 15 && $mon == 12) :
            $bnMon = 7;
            $bnDay = $day + 15;
        elseif($day > 15 && $mon == 12) :
            $bnMon = 8;
            $bnDay = $day - 15;
        elseif($day <= 14 && $mon == 1) :
            $bnMon = 8;
            $bnDay = $day + 16;
            $yx = 1;
        elseif($day > 14 && $mon == 1) :
            $bnMon = 9;
            $bnDay = $day - 14;
            $yx = 1;
        elseif($day <= 13 && $mon == 2) :
            $bnMon = 9;
            $bnDay = $day + 17;
            $yx = 1;
        elseif($day > 13 && $mon == 2) :
            $bnMon = 10;
            $bnDay = $day - 13;
            $yx = 1;
        elseif($day <= (15-$ly) && $mon == 3) :
            $bnMon = 10;
            $bnDay = $day + 15 + $ly;
            $yx = 1;
        elseif($day > (15-$ly) && $mon == 3) :
            $bnMon = 11;
            $bnDay = $day - (15-$ly);
            $yx = 1;
        elseif($day < 14 && $mon == 4) :
            $bnMon = 11;
            $bnDay = $day + 16 + $ly;
            $yx = 1;
        endif;
        
        $bnDay = $this->toBnNum($bnDay);
        $bnMon = $arrBnMon[$bnMon];
        $bnYear = $this->toBnNum($year - 593 - $yx);
        return $bnDay .' '.$bnMon.', '.$bnYear;
    }
    
    function mysql2php($str)  {
        list($date, $time) = explode(' ', $str);
        list($year, $month, $day) = explode('-', $date);
        list($hour, $minute, $second) = explode(':', $time);

        $timestamp = mktime($hour, $minute, $second, $month, $day, $year);

        return $timestamp;
    }
    
    function timeSince($start, $end = null){
        if($end==null) $end = time();
        //both times must be in seconds
        $diff = $end - $start;
        $chunks = array(12*30*60*60*24, 30*60*60*24, 60*60*24, 60*60, 60);
        
        $time = array();
        foreach($chunks as $v) {
            $time[] = (int)($diff / $v);
            $diff %= $v;
        }
        
        $str = array('Years', 'Months', 'Days', 'Hours', 'Minutes');
        $out = array();
        
        foreach($time as $i => $v) {
            if($v) $out[] = $this->toBnNum($v).' '.__($str[$i], true);
        }
        
        return implode($out, ', ');
    }
    
    function date($dateString) {
        return strftime('%d %B %Y, %H:%M', $this->Time->fromString($dateString));
    }
    
    
    function profileUrl($user) {
        if(!empty($user['User']['username'])) return array(
            'controller' => 'users', 
            'action' => 'profile', 
            $user['User']['username'], 
            'admin' => false,
        );
        return '';
    }
    
      
}
?>